<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>制定入库单</title>

    <link rel="stylesheet" href="../css/pintuer.css">
    <link rel="stylesheet" href="../css/admin.css">
    <link rel="stylesheet" href="../css/bootstrap.css">

    <script src="../js/jquery-3.4.1.min.js"></script>
    <script src="../js/bootstrap-table1.15.3.js"></script>
    <script src="../js/bootstrap-table-zh-CN.min.js"></script>
    <script src="../js/popper.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>

    <style>
        td,tr,table{
            text-align: center;
        }
        input{
            border-top: none;
            border-left: none;
            border-right: none;
            border-bottom: black solid 1px;
        }
        table{
            border: 1px;
            cell-padding:0;
            cell-spacing:0;
            width: 80%;
            margin-top: 20px;
        }
        table thead{
            background-color:#b3d7ff ;
        }
    </style>

</head>

<script>

    //父级ID
    var parentId;
    //最大库存容量
    var maxCapacityAmount;

    function initPutSetSelect(){
        $('#putSetSelectTableInfo').bootstrapTable(
            {
                //请求方式
                method:'post',
                contentType:'application/json',
                url:'/SGather/queryPutSetAll.action',
                //设置交替样式
                striped:true,
                //初始显示页号
                pageNumber:1,
                //是否显示分页
                pagination:true,
                //是否设置服务端分页还是客户端分页
                sidePagination:'server',
                //页面大小
                pageSize:3,
                //分页列表
                pageList:[1,3,5,6,10],
                //上传到到服务端的参数
                queryParams:function (pares) {
                    return{
                        //计算页号（当前页）
                        pageNum:(pares.offset/pares.limit)+1,
                        //计算页大小
                        pageSize:pares.limit,
                    }
                },
                //第一列名
                columns:[
                    {title:"申请单编号",field:"gatherId",align:"center"},
                    {title:"入库理由",field:"reason",align:"center",formatter:function (value,row,index) {
                            if(value=="R001-1"){
                                return "生产入库";
                            }else if(value=="R001-2"){
                                return "库存初始";
                            }else if(value=="R001-3"){
                                return "赠送";
                            }else if(value=="R001-4"){
                                return "内部归还";
                            }else if(value=="R001-5"){
                                return "其他归还";
                            }
                        }},
                    {title:"登记时间",field:"registerTime",align:"center"},
                    {title:"总件数",field:"amountSum",align:"center"},
                    {title:"总金额",field:"costPriceSum",align:"center"},
                    {title:"审核",align:"center",formatter:function (value,row,index) {
                            return "<a href='#' onclick='updateRegister("+JSON.stringify(row)+")' data-toggle='modal' data-target='#putSetModal'>入库调度</a>"
                        } }
                ]
            })
    }

    $(function () {
        initPutSetSelect();

        //入库调度
        $('#putSubmitBtn').click(function () {
            // alert(parentId)
            //本次入库数量
            let gatheredAmount = $('#gatheredAmount').val();
            //产品编号
            let productId = $('#productId').val();
            //应入库的数量
            let amount = $('#amount').val();
            //当前可存放数量
            // let sCellAmount = $('#sCellAmountTd').text();
            //登记时间
            let registerTime = $('#registerTime1').val();
            if(maxCapacityAmount<gatheredAmount){
                alert('本次加入库存的数量，已超过可容纳的库存量')
            }else if(amount != gatheredAmount){
                alert('您输入的库存数量不对')
            }else{
                $.ajax({
                    type:'post',
                    url:'/sGatherDetails/updateSGatherDetailsByProductId',
                    data:'gatheredAmount='+gatheredAmount+'&productId='+productId+
                        '&registerTime='+registerTime+'&parentId='+parentId,
                    success:function () {
                        alert('调度成功')
                        $('#putSetSubmitModal').modal('hide');
                        $('#'+resId).text('已调度');
                        $('#putSetSelectTableInfo').bootstrapTable('refresh')
                    },
                    error:function () {
                        alert('调度失败')
                    }
                })
            }
        })

    })

    var resId;
    //将数据添加到 “入库申请单” 的表格中
    function updateRegister(rows) {

        $('#gatherId').val(rows.gatherId);
        $('#storer').val(rows.storer);
        let reason = rows.reason;
        if(reason=='R001-1'){
            reason='生产入库';
        }else if(reason=='R001-2'){
            reason='库存初始';
        }else if(reason=='R001-3'){
            reason='赠送';
        }else if(reason=='R001-4'){
            reason='内部借领';
        }else if(reason=='R001-5'){
            reason='其他借领';
        }
        $('#reason').val(reason);
        $('#amountSum').val(rows.amountSum);
        $('#costPriceSum').val(rows.costPriceSum);

        parentId=rows.id;

        $('#tbodyId').empty();
        $.ajax({
            type:'post',
            url:'/sGatherDetails/querySGatherDetailsAll.action',
            data:'parentId='+rows.id,
            success:function (result) {
                let tbodyId = $('#tbodyId');
                $.each(result,function (i,res) {
                    let trObj = $('<tr>');
                    tbodyId.append(trObj);
                    trObj.append($('<td>').text(i+1));
                    trObj.append($('<td>').text(res.productName));
                    trObj.append($('<td>').text(res.productId));
                    //数量
                    trObj.append($('<td>').text(res.amount));
                    // trObj.append($('<td>').text(res.gatheredAmount));
                    if(res.gatherTag=='K002-2'){
                        trObj.append($('<td>').text('已调度'))
                    }else {
                        trObj.append($('<td id="'+res.id+'">').html("<a href='#' onclick='updateSGatherDetails("+JSON.stringify(res)+")' data-toggle='modal' data-target='#putSetSubmitModal'>调度</a>"));
                    }
                })
            }
        });
    }

    function updateSGatherDetails(res) {
        resId=res.id;
        $('#gatherId1').val($('#gatherId').val());
        $('#productId').val(res.productId);
        //登记时间
        var today=new Date();
        let year = today.getFullYear();
        let month = today.getMonth();
        let date = today.getDate();
        let hours = today.getHours();
        let minutes = today.getMinutes();
        let seconds = today.getSeconds();
        today=year+"-"+(month+1)+"-"+date+" "+hours+":"+minutes+":"+seconds;
        //登记时间
        $('#registerTime1').val(today);

        $('#tbodyId1').empty();
        $.ajax({
            type:'post',
            url:'/sGatherDetails/querySGatherDetailsByParentIdByProductId.action',
            data:'parentId='+parentId+'&productId='+res.productId,
            success:function (result) {
                $('#productName').val(result.productName)
                $('#costPrice').text(result.costPrice)
                var tbodyIdObj = $('#tbodyId1')
                var trObj = $('<tr>')
                tbodyIdObj.append(trObj)
                trObj.append($('<td>').text(1))
                trObj.append($('<td id="sCellAmountTd">').text(result.sCell.maxCapacityAmount))
                trObj.append($('<td>').html('<input size="5px" id="gatheredAmount">'))

                //登记人
                // alert(result.sCell.register)
                $('#register1').val(result.sCell.register)
                //应入库
                $('#amount').val(res.amount)
                //应入库成本
                $('#subtotal').val(result.subtotal)

                //获取最大库存容量
                maxCapacityAmount=result.sCell.maxCapacityAmount;
            }
        })
    }

</script>


<body bgcolor="#f5f5f5">

<div class="container-fluid">
    <div style="width: 100%;margin: 0px;padding: 0px;">
        <table id="putSetSelectTableInfo" class="table table-bordered table-hover table-striped table-condensed">

        </table>
    </div>
</div>

<!--模态框-->
<!--modal fade-->
<div class="modal  fade" id="putSetModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <form id="updateSGatherFormId">
        <div class="modal-dialog" style="width: 90%">
            <div class="modal-content" style="border: 1px solid black;">
                <div class="modal-header">
                    <div class="container">
                        <div class="row">
                            <div class="col-7" style="text-align: right">
                                <h5 class="modal-title" id="info" >
                                    入库调度单
                                </h5>
                            </div>
                        </div>
                    </div>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 0px">

                    </button>
                </div>
                <div class="modal-body">
                    <div class="container-fluid">
                        <div class="row" style="margin-top: 10px">
                            <div class="col-12">
                                <div class="container-fluid">
                                    <div class="row">
                                        <div class="col-4">
                                            入库单编号:
                                            <input name="gatherId" id="gatherId"/>
                                        </div>
                                    </div>
                                    <div class="row" style="margin-top: 30px;">
                                        <div class="col-4">
                                            入库人：<input name="storer" id="storer">
                                        </div>

                                        <div class="col-4 offset-2">
                                            入库理由：<input id="reason" id="reason">
                                        </div>
                                    </div>

                                    <div>
                                        <table border="1" cellpadding="0" cellspacing="0" width="80%" style="margin-top: 20px;" class="table table-sm">
                                            <thead  style="background-color: #b3d7ff;">
                                            <tr>
                                                <th>序号</th>
                                                <th>产品名称</th>
                                                <th>产品编号</th>
                                                <th>应入库件数</th>
<!--                                                <th>已入库件数</th>-->
<!--                                                <th>存放地址集合</th>-->
                                                <th>调度</th>
                                            </tr>
                                            </thead>
                                            <tbody id="tbodyId">

                                            </tbody>
                                        </table>
                                    </div>

                                    <div class="row" style="margin-top: 30px;">
                                        <div class="col-4">
                                            应入库总件数：<input name="amountSum" id="amountSum">
                                        </div>

                                        <div class="col-4 offset-2">
                                            应入库总金额：<input name="costPriceSum" id="costPriceSum">
                                        </div>

                                    </div>

                                    <!--<div class="row" style="margin-top: 30px;">
                                        <div class="col-3">
                                            已入库总件数：0.0
                                        </div>

                                        <div class="col-3 offset-3">
                                            已入库总成本：0.0
                                        </div>
                                    </div>-->
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </form>
</div>

<!--模态框-->
<!--modal fade-->
<div class="modal  fade" id="putSetSubmitModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="width: 90%">
            <div class="modal-content" style="border: 1px solid black;">
                <div class="modal-header">
                    <div class="container">
                        <div class="row">
                            <div class="col-7" style="text-align: right">
                                <h5 class="modal-title">
                                    入库申请单
                                </h5>
                            </div>
                        </div>
                    </div>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 0px"></button>
                </div>
                <div class="modal-body">
                    <div class="container-fluid">
                        <div class="row" style="margin-top: 10px">
                            <div class="col-12">
                                <div class="container-fluid">
                                    <div class="row">
                                        <div class="col-4">
                                            入库单编号:<input name="gatherId" id="gatherId1"/>
                                        </div>
                                        <div class="col-4 offset-2">
                                            产品编号：<input name="productId" id="productId">
                                        </div>
                                    </div>
                                    <div class="row" style="margin-top: 30px;">
                                        <div class="col-4">
                                            产品名称：<input name="productName" id="productName">
                                        </div>
                                        <div class="col-4 offset-2">
                                            成本单价：<span id="costPrice"></span>
                                        </div>
                                    </div>

                                    <div>
                                        <table border="1" cellpadding="0" cellspacing="0" width="80%" style="margin-top: 20px;" class="table table-sm">
                                            <thead  style="background-color: #b3d7ff;">
                                            <tr>
                                                <th>序号</th>
                                                <!--<th>库房名称</th>
                                                <th>存储地址编号</th>
                                                <th>存储地址名称</th>
                                                <th>存储单元简称</th>-->
                                                <th>当前可存放数量</th>
                                                <th>本次入库数量</th>
                                            </tr>
                                            </thead>
                                            <tbody id="tbodyId1">

                                            </tbody>
                                        </table>
                                    </div>

                                    <div class="row" style="margin-top: 30px;">
                                        <div class="col-4">
                                            登记人：<input name="register" id="register1">
                                        </div>
                                        <div class="col-4 offset-2">
                                            登记时间：<input name="registerTime" id="registerTime1">
                                        </div>
                                    </div>

                                    <div class="row" style="margin-top: 30px;">
                                        <div class="col-4">
                                            应入库：<input name="amount" id="amount">
                                        </div>
                                        <div class="col-4 offset-2">
                                            应入库成本：<input name="subtotal" id="subtotal">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" id="putSubmitBtn">
                        确认调度
                    </button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
</div>

</body>
</html>